package com.jiazixuan.stock.pojo.domain;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.jiazixuan.stock.pojo.entity.SysPermission;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
/**
 * @Description: TODO:侧边栏权限树（不包含按钮权限）
 */
@ApiModel(description = "侧边栏权限树（不包含按钮权限）")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MenusPermDomain {
    //权限ID
    @ApiModelProperty(value = "权限ID", position = 1)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    //权限标题
    @ApiModelProperty(value = "权限标题", position = 2)
    private String title;
    //权限图标（按钮权限无图片）
    @ApiModelProperty(value = "权限图标（按钮权限无图片）", position = 3)
    private String icon;
    //请求地址
    @ApiModelProperty(value = "请求地址", position = 4)
    private String path;
    //权限名称对应前端vue组件名称
    @ApiModelProperty(value = "权限名称对应前端vue组件名称", position = 5)
    private String name;
    //子权限树
    @ApiModelProperty(value = "子权限树", position = 6)
    private List<MenusPermDomain> children;

    //递归各层级权限获取权限树
    public static List<MenusPermDomain> digui(List<SysPermission> permissions, Long pid){
        //创建集合用于存放递归树
        ArrayList<MenusPermDomain> list = new ArrayList<>();
        //遍历权限集合
        for (SysPermission p : permissions) {
            //判断p的pid是哪一个父类
            if (p.getPid().equals(pid)) {
                //过滤按钮权限
                if (p.getType().intValue() != 3) {
                    list.add(new MenusPermDomain(p.getId(), p.getTitle(), p.getIcon(), p.getUrl(), p.getName(), digui(permissions, p.getId())));
                }
            }
        }
        return list;
    }
    //不过滤按钮
    public static List<MenusPermDomain> digui2(List<SysPermission> permissions, Long pid){
        //创建集合用于存放递归树
        ArrayList<MenusPermDomain> list = new ArrayList<>();
        //遍历权限集合
        for (SysPermission p : permissions) {
            //判断p的pid是哪一个父类
            if (p.getPid().equals(pid)) {
                list.add(new MenusPermDomain(p.getId(), p.getTitle(), p.getIcon(), p.getUrl(), p.getName(), digui2(permissions, p.getId())));
            }
        }
        return list;
    }

}
